(:
   Functions that are common to all coffee messages
 :)
module namespace coffee = "http://tatiana.emse.fr/coffee" ;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;

declare function coffee:get-messages($filedoc as node()*) as node()*
{
	$filedoc//record/message
}

declare function coffee:get-tool($msg as element()*) as xs:string*
{
	fn:tokenize(string($msg//ServiceInfo/@ServiceID),"_")[1]
};

declare function coffee:get-group($msg as element()*) as xs:string*
{
	fn:tokenize(string($msg/ServiceInfo/configuration/@ID),"-")[2]
};

declare function coffee:get-step($msg as element()*) as xs:string*
{
	fn:tokenize(string($msg/ServiceInfo/configuration/@ID),"-")[1]
};

declare function coffee:get-username($msg as element()*) as xs:string*
{
	string(jj:get-one($msg//@from,$msg/messageHeader/@UserName))
};

declare function coffee:get-timemillis($msg as element()*) as xs:string
{
	string($msg//@millis[1])
};

declare function coffee:get-seqnum($msg as element()*) as xs:string*
{
	string($msg/messageHeader/@sequenceNumber)
};

(: replace weird characters with spaces :)
declare function coffee:string($value as node()*) as xs:string*
{
	let $ovag := fn:string-join(fn:tokenize(xs:string($value),"#ovag;"),"    ")
	let $tab := fn:string-join(fn:tokenize($ovag,"#tab;"),"    ")
	return $tab
};

declare function coffee:get-version($filedoc as node()*) as xs:integer
{
	if ($filedoc//record[1]//timestamp) then 100 (:alpha:)
	else if ($filedoc//record[1]/message//messageTimestamp) then 104 (:alpha+:)
	else  200 (:1.6, 1.7, beta:)
} 